<script>
function show_countdown(auction_id,until_time){
	var timer_countdown_selector = '#'+auction_id+' .timer_countdown';
	// Convert MySql DateTime stamp into JavaScript's Date format
	var t = until_time.split(/[- :]/);
	var liftoffTime = new Date(t[0], t[1]-1, t[2], t[3], t[4], t[5]);
	$(timer_countdown_selector).countdown({until: liftoffTime, format: 'HMS', significant : 3, layout: '{hn} {hl} {mn} {ml} {sn} {sl}', onExpiry: timer_onExpiry});
}
function timer_onExpiry(){
	var timer_countdown_jele = $(this);
	var auction_id = timer_countdown_jele.attr('id');
	if(auction_id){
		setTimeout(function(){
			timer_onExpiry_inner(timer_countdown_jele,auction_id);
		},5000);
		
	}
}
function timer_onExpiry_inner(timer_countdown_jele,auction_id){
	$.post('<?=PATH_URL?>auction/result',{'auction_id':auction_id},function(data){
		var parent = timer_countdown_jele.parent();
		if(parent.hasClass('timer_countdown_container')){
			parent.addClass('timer_result_container')
		}
		try {
			var return_data = $.parseJSON(data);
			timer_countdown_jele.html(return_data.html);
			$('#auction_'+auction_id+' .CurrentPrice').html(return_data.winner_price);
		}catch(ex){}
	});
}
</script>

<div id="messageInformationOpening" class="messageInformation open" style="margin-bottom: 3px;">
	<!-- <a href="<?=PATH_URL?>static/e-auction/Y/Bid/List/" class="mode listView">Danh sách</a>
	<a href="<?=PATH_URL?>static/e-auction/Y/Bid/" class="mode thumbnailView active">Hình thu nhỏ</a> -->
	<span class="if ifOPEN"><strong>Thời gian</strong></span> của mỗi đấu giá được quy định khác nhau <span class="low">(theo giờ Việt Nam)</span>
</div>
<?php
if(!empty($auction_list[0])){
	$auction = $auction_list[0]; // First auction
	if(empty($auction['is_ended'])){
		$auction_id = 'auction_'.$auction['id'];
?>
<div id="AuctionBlockBigSize">
	<div class="AuctionBlockContainer" id="BigAuctionBlock">
		<div id="<?=$auction_id?>" class="auction auctionBigBlock Auction Auction_status_INPLAY">
			<div class="auctionDetailsContainer">
				<div class="productPictureContainer">
					<a href="<?=get_detail_url_for_auction($auction)?>" class="Zoom">
						<img src="<?=get_image_url_for_auction($auction,'large')?>" class="productPicture"/>
					</a>
				</div>
				<h1 class="productName"><a href="<?=get_detail_url_for_auction($auction)?>"><?=$auction['title']?></a></h1>
				<div class="auctionDetails">
					<div class="auctionInfos">
						<div class="productPriceContainer">
							<div class="productPrice ifNotNOBIDDER ifNotDEALAUCTION"><span class="CurrentPrice">0</span> <?=MONETARY_UNIT?></div>
							<div class="productValue ifNotDEALAUCTION">Giá : <strong><?=get_price_for_auction($auction)?> <?=MONETARY_UNIT?></strong></div>
						</div>
						<div class="productTimerContainer">
							<div class="timer Timer if ifINPLAY ifIMMINENT ifNotCHECKING ifNotSUSPENDED ifNotTERMINATED ifNotPAID ifNotREFUNDED ifNotCANCELED timer_countdown_container" style="">
								<div class='timer_countdown' id='<?=$auction['id']?>'></div>
								<script>
									show_countdown('<?=$auction_id?>','<?=$auction['end_time']?>');
								</script>
							</div>
						</div>
					</div>
					<?php
					if(!empty($_SESSION['user'])){
					?>
					<div class="auctionButtons">
						<div class="ifNotDEALAUCTION">
							<div class="auctionButton auctionButtonPlaceBid placeBid if ifINPLAY ifNotHOLD">
								<a href="<?=get_detail_url_for_auction($auction)?>">
									<div></div>
								</a>
							</div>
						</div>
					</div>
					<?php
					}
					?>
					<div class="ifNotDEALAUCTION"></div>
				</div>
			</div>
		</div>
	</div>
</div>
<?php
	}
}
?>

<?php
if(!empty($auction_list) && count($auction_list) > 1){
?>
<div id="AuctionBlockNormalSize">
	<?php
	for($i=1;$i<count($auction_list);$i++){
		$auction = $auction_list[$i];
		$auction_id = 'auction_'.$auction['id'];
	?>
	<div id="AuctionBlock" class="AuctionBlockContainer normalAuctionContainer">
		<div class="auction auctionBlock Auction Auction_status_IMMINENT Auction_status_PENDING Auction_status_NOBIDDER" id="<?=$auction_id?>">
		   <h2 class="productName"><a href="<?=get_detail_url_for_auction($auction)?>"><?=$auction['title']?></a></h2>
		   <div class="auctionDetailsContainer">
				<div class="productPictureContainer">
					<a href="<?=get_detail_url_for_auction($auction)?>">
						<img class="productPicture" src="<?=get_image_url_for_auction($auction)?>" />
					</a>
				</div>
				<div class="auctionDetails">
					<div class="auctionInfos">
						<div class="productPriceContainer">
						   <div class="productPrice if ifNOBIDDER ifNotLOADING ifNotDEALAUCTION">0,00 <?=MONETARY_UNIT?></div>
						   <div class="productValue ifNotDEALAUCTION">Giá : <strong><?=get_price_for_auction($auction)?> <?=MONETARY_UNIT?></strong></div>
						</div>
						<div class="productTimerContainer">
							<div class="timer Timer if ifINPLAY ifIMMINENT ifNotCHECKING ifNotSUSPENDED ifNotTERMINATED ifNotPAID ifNotREFUNDED ifNotCANCELED Delay_HOUR">
								<?php
								if(!empty($auction['is_ended'])){
								?>
									<div class='timer_countdown'>Đã kết thúc!</div>
								<?php
								} else {
								?>
								<div class='timer_countdown' id='<?=$auction['id']?>'></div>
								<script>
									show_countdown('<?=$auction_id?>','<?=$auction['end_time']?>');
								</script>
								<?php
								}
								?>
							</div>
						</div>
					</div>
					<?php
					if(!empty($_SESSION['user']) && empty($auction['is_ended'])){
					?>
					<div class="auctionButtons">
						<div class="ifNotDEALAUCTION">
						   <div class="auctionButton auctionButtonSetAutoBid if ifPENDING ifIMMINENT ifSUSPENDED ifNotHOLD">
							  <a href="<?=get_detail_url_for_auction($auction)?>">
								<div></div>
							  </a>
						   </div>
						</div>
					</div>
					<?php
					}
					?>
				</div>
		   </div>
		</div>
	</div>
	<?php
	}
	?>
</div>
<?php
}
?>